package com.example.park2212.devices

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.AdapterView
import android.widget.AdapterView.OnItemSelectedListener
import android.widget.ArrayAdapter
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.ToastUtils
import com.example.lib_base.entity.DevicesBeanItem
import com.example.lib_base.mvvm.BaseActivity
import com.example.park2212.R
import com.example.park2212.databinding.ActivityDevicesAddBinding
import com.example.park2212.repair.RepairImgAdapter
import com.wildma.pictureselector.PictureBean
import com.wildma.pictureselector.PictureSelector
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
@Route(path = "/app/devices/DevicesAddActivity")
class DevicesAddActivity : BaseActivity<ActivityDevicesAddBinding,DevicesVewModel>() {
    lateinit var repairImgAdapter: RepairImgAdapter
    var type = ""
    var status = ""
    override fun initView() {
        binding.devicesAddHead.headerTitle.setText("新增检查工单")
        binding.devicesAddHead.headerBack.setOnClickListener {
            finish()
        }
        val list = mutableListOf<String>("门禁设备","监控设备","消防设备")
        val adapter = ArrayAdapter(this,android.R.layout.simple_spinner_item,list)
        binding.devicesAddSptype.adapter = adapter
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
        binding.devicesAddSptype.onItemSelectedListener = object :OnItemSelectedListener{
            override fun onItemSelected(p0: AdapterView<*>?, p1: View?, p2: Int, p3: Long) {
                type = list[p2]
            }

            override fun onNothingSelected(p0: AdapterView<*>?) {

            }

        }

        val list1 = mutableListOf<String>("正常","故障")
        val adapter1 = ArrayAdapter(this,android.R.layout.simple_spinner_item,list1)
        binding.devicesAddSpstatus.adapter = adapter1
        adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
        binding.devicesAddSpstatus.onItemSelectedListener = object :OnItemSelectedListener{
            override fun onItemSelected(p0: AdapterView<*>?, p1: View?, p2: Int, p3: Long) {
                status = list1[p2]
            }

            override fun onNothingSelected(p0: AdapterView<*>?) {

            }

        }

        binding.btnTj.setOnClickListener {
            val etAddress = binding.devicesAddEtaddress.text.toString()
            val etName = binding.devicesAddEtname.text.toString()

            val beanItem = DevicesBeanItem(etName, type, 0.2, 2, etAddress, status)
            viewModel.addDevices(beanItem)
        }

        repairImgAdapter = RepairImgAdapter()
        binding.rvImg.adapter = repairImgAdapter
        binding.rvImg.layoutManager = LinearLayoutManager(this,RecyclerView.HORIZONTAL,false)

        binding.devicesAddTvpotos.setOnClickListener {
            PictureSelector.create(this,PictureSelector.SELECT_REQUEST_CODE).selectPicture()
        }


    }

    override fun initData() {
        viewModel.addDevicesSuccess.observe(this){
            ToastUtils.showLong("添加成功")
            finish()
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == PictureSelector.SELECT_REQUEST_CODE && resultCode == RESULT_OK){
            val pictureBean = data?.extras?.getParcelable<PictureBean>(PictureSelector.PICTURE_RESULT)
            repairImgAdapter.addData(pictureBean?.path!!)
            repairImgAdapter.notifyDataSetChanged()
        }
    }

}